package lacus.wd_cinema.repository;

import lacus.wd_cinema.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

/**
 * 与用户相关的数据访问类
 * @author lacus
 * @version 0.1
 * @since 0.1
 */
@Repository
public interface UserRepository extends JpaRepository<User, Long> {

    boolean existsByUsername(String username);

    Page<User> findAllByUsernameContaining(String username, Pageable pageable);

    @Query("SELECT u FROM User u WHERE CAST(u.id AS string) LIKE %:keyword%")
    Page<User> findAllByIdContaining(@Param("keyword") String keyword, Pageable pageable);

    Page<User> findAllByPhoneNumberContaining(String keyword, Pageable pageable);

    Optional<User> findByUsername(String username);
}
